package interfazeak;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import db.BarietateaKud;
import db.UpelaKud;

import Imagenes.IrudiakUpategia;
import Taulak.MTBMahastiak;
import Taulak.MTBUpela;

public class Upategia extends JFrame {
	private JPanel contentPane;
	private JButton btnHasieraOrria;
	private JTable table;
	private MTBUpela upelataula;
	private JButton gehitu;
	private JTextArea barietatetxt;
	private JLabel barietateak;
	private JTextArea tratatxt;
	private JLabel tratamenduak;
	private JButton tratatu;
	private JButton ezabatu;
	private JButton filtratu;
	private JButton enboteilaratu;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Upategia frame = new Upategia();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Upategia() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("UPATEGIA");
		setBounds(100, 100, 800, 550);
		
		IrudiakUpategia p = new IrudiakUpategia();
		p.setBorder(new EmptyBorder(5, 5, 5, 5));
		p.setLayout(new BorderLayout(0, 0));
		setContentPane(p);
		p.setLayout(null);

		
		btnHasieraOrria = new JButton("HASIERA ORRIA");
		btnHasieraOrria.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				Upategia.this.buelta();
			}
		});
		btnHasieraOrria.setBounds(500, 20, 157, 25);
		p.add(btnHasieraOrria);

		enboteilaratu = new JButton("ENBOTEILARATU");
		enboteilaratu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Upategia.this,
							"Lerro bat aukeratu", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);

				} else {
				
					Upategia.this.enboteilaratu();
				

				}
			}
		});
		enboteilaratu.setBounds(100, 20, 157, 25);
		p.add(enboteilaratu);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(20, 50, 500, 250);
		p.add(scrollPane);

		table = new JTable();
		upelataula = new MTBUpela();
		table.setModel(upelataula);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					JTable target = (JTable) e.getSource();
					int row = target.getSelectedRow();
					// int column = target.getSelectedColumn();
					Upategia.this.barietateakerakutsi(row);
					Upategia.this.tratamenduakerakutsi(row);
				}
			}
		});
		scrollPane.setViewportView(table);
		table.setOpaque(false);
		scrollPane.setOpaque(false);
		scrollPane.getViewport().setOpaque(false);

		

		table.setShowGrid(false);
		
		gehitu = new JButton("EROSI");
		gehitu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Upategia.this.gehitu();
			}
		});
		gehitu.setForeground(Color.BLACK);
		gehitu.setBounds(270, 20, 157, 25);
		p.add(gehitu);

		ezabatu = new JButton("EZABATU");
		ezabatu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Upategia.this,
							"Lerro bat aukeratu", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				} else {
					int konfirmatu = JOptionPane.showConfirmDialog(
							Upategia.this, "Ezabatu nahi duzu?");

					if (JOptionPane.OK_OPTION == konfirmatu) {
						Upategia.this.ezabatu();
					}
				}
			}
		});

		ezabatu.setForeground(Color.BLACK);
		ezabatu.setBounds(535, 125, 117, 25);
		p.add(ezabatu);

		filtratu = new JButton("FILTRATU");
		filtratu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				

				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Upategia.this,
							"Lerro bat aukeratu", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);

				} else {
				
						Upategia.this.filtratu();
				

				}
			}

		});
		filtratu.setForeground(Color.BLACK);
		filtratu.setBounds(535, 225, 117, 25);
		p.add(filtratu);

		tratatu = new JButton("TRATATU");
		tratatu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Upategia.this,
							"Lerro bat aukeratu", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);

				} else {

					Upategia.this.tratatu();

				}
			}
		});
		tratatu.setBounds(535, 175, 117, 25);
		p.add(tratatu);

		JScrollPane scroll = new JScrollPane();
		scroll.setBounds(20, 350, 200, 100);
		p.add(scroll);

		barietatetxt = new JTextArea();
		barietatetxt.setEditable(false);
	
		p.add(barietatetxt);
		scroll.setViewportView(barietatetxt);

		barietateak = new JLabel("BARIETATEAK");
		barietateak.setForeground(Color.WHITE);
		barietateak.setBounds(35, 320, 100, 25);
		p.add(barietateak);

		JScrollPane scroll2 = new JScrollPane();
		scroll2.setBounds(300, 350, 300, 100);
		p.add(scroll2);

		tratatxt = new JTextArea();
		tratatxt.setEditable(false);
		p.add(tratatxt);
		scroll2.setViewportView(tratatxt);

		tratamenduak = new JLabel("TRATAMENDUAK");
		tratamenduak.setForeground(Color.WHITE);
		tratamenduak.setBounds(300, 320, 120, 25);
		p.add(tratamenduak);
		kargatu();
	}

	protected void enboteilaratu() {
		
		int lerroa = table.getSelectedRow();
		int upelaz = (int) table.getValueAt(lerroa, 1);
		int litroak = (int) table.getValueAt(lerroa, 3);
		JDialog dialog = new Enboteilaratu(Upategia.this, true, upelaz, litroak);
		dialog.setVisible(true);
	}

	protected void filtratu() {
		
		int lerroa = table.getSelectedRow();
		int upelaz = (int) table.getValueAt(lerroa, 1);
		int betelitro = (int) table.getValueAt(lerroa, 3);
		JDialog dialog = new Filtratu(Upategia.this, true, upelaz, betelitro);
		dialog.setVisible(true);

	}

	protected void tratamenduakerakutsi(int row) {
		
		tratatxt.setText("");
		int upelaz = (int) table.getValueAt(row, 1);


		ResultSet rs1 = UpelaKud.lortuupelarenTratamenduak(upelaz);

		try {
			while (rs1.next()) {
				Date data = rs1.getDate("data");
				String tratamendua = rs1.getString("tratamendua");
				System.getProperty("line.separator");
				tratatxt.append(tratamendua + ":  " + data + "\n");
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}

	}

	protected void barietateakerakutsi(int row) {
		
		barietatetxt.setText("");
		int upelaz = (int) table.getValueAt(row, 1);
		ResultSet rs = UpelaKud.lortuidUpelatzeak(upelaz);
		try {
			while (rs.next()) {
				int barietateid = rs.getInt("barietateid");
				String barietate = BarietateaKud.lortuBarietatea(barietateid);
				int ehuneko = rs.getInt("ehuneko");
				System.getProperty("line.separator");
				barietatetxt.append(barietate + ":  " + ehuneko + "%" + "\n");

			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	protected void tratatu() {
				int lerroa = table.getSelectedRow();
		int upelaz = (int) table.getValueAt(lerroa, 1);
	//	ResultSet rs = UpelakKud.lortuidUpelatzeak(upelaz);
		JDialog dialog = new Tratatu(Upategia.this, true, upelaz);
		dialog.setVisible(true);
	}

	protected void ezabatu() {
				int lerroa = table.getSelectedRow();
		int upelaz = (int) table.getValueAt(lerroa, 1);
		UpelaKud.ezabatu(upelaz);
		upelataula.ezabatu(lerroa);
	}

	public void kargatu() {

		ResultSet rs = UpelaKud.bistaratuUpelak();
		int zenbakia = 0;
		try {
			while (rs.next()) {
				int upelazenb = rs.getInt("upelazenb");
				if (upelazenb == zenbakia) {

				} else {
					int edukiera = UpelaKud.lortuUpelarenEdukiera(upelazenb);
					int txakozenb = rs.getInt("txakolinazenb");
					int bete = rs.getInt("betetakolitroak");
					upelataula.kargatu(txakozenb, upelazenb, edukiera, bete);
					zenbakia = upelazenb;
				}
				;
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}

	}

	protected void gehitu() {
		
		upelaGehitu b = new upelaGehitu(Upategia.this);
		b.setVisible(true);
		this.dispose();
	}

	protected void buelta() {
				Hasiera h = new Hasiera();
		h.setVisible(true);
		this.dispose();
	}

	public void ezabatutaula() {
				int ler = table.getRowCount();
		upelataula.taulaHasieratu(ler);
	}

}
